using System;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using Microsoft.WowAddonStudio.FrameXml.Components.Editors;

namespace Microsoft.WowAddonStudio.FrameXml.Components.Events.Editors
{
    public class EventSelectorUI : ListBox
    {
        private readonly IServiceProvider provider;

        /// <summary>
        /// Initializes a new instance of the <see cref="ControlSelectorUI"/> class.
        /// </summary>
        /// <param name="provider">The provider.</param>
        public EventSelectorUI(IServiceProvider provider)
        {
            this.provider = provider;

            InitializeComponent();

            FillControls();
        }

        /// <summary>
        /// Fills the controls.
        /// </summary>
        protected virtual void FillControls()
        {
            //TODO: Implement list of events to show.
        }

        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>The value.</value>
        public virtual object Value { get; set; }

        /// <summary>
        /// Initializes the component.
        /// </summary>
        protected virtual void InitializeComponent()
        {
            BorderStyle = BorderStyle.None;
            SelectionMode = SelectionMode.One;
            SelectedIndexChanged += UISelectedIndexChanged;
        }

        /// <summary>
        /// UIs the selected index changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void UISelectedIndexChanged(object sender, EventArgs e)
        {
            UISelectedIndexChanged(e);
        }

        /// <summary>
        /// UIs the selected index changed.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected virtual void UISelectedIndexChanged(EventArgs e)
        {
            Value = SelectedItem;

            var editorService = (IWindowsFormsEditorService) provider.
                                                                 GetService(typeof (IWindowsFormsEditorService));
            if (editorService != null)
            {
                editorService.CloseDropDown();
            }
        }
    }
}